package com.itheima.web.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/30
 */
//全局异常处理类
@ControllerAdvice
public class GlobalExceptionAdvice {

    //定义专门处理业务异常的方法
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity businessHandler(Exception e){
        Map<String,String> map = new HashMap<>();
        map.put("message", e.getMessage());
        return ResponseEntity.status(500).body(map);
    }

    //定义处理其他异常的方法
    @ExceptionHandler(Exception.class)
    public ResponseEntity  globalHandler(Exception e){
        Map<String ,String> map = new HashMap<>();
        map.put("message" , "未知异常,请联系管理员");
        return ResponseEntity.status(500).body(map);
    }



}
